package cn.luckdou.system.controller.we_chat_event;

import java.util.List;
import java.util.Date;

import cn.luckdou.commons.base.BaseController;
import cn.luckdou.commons.result.PageInfo;
import cn.luckdou.commons.utils.Des3Util;
import cn.luckdou.commons.utils.JsonUtils;
import cn.luckdou.system.model.we_chat_event.WeChatEvent;
import cn.luckdou.system.service.we_chat_event.IWeChatEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.log4j.Logger;
import org.apache.commons.lang.StringUtils;

/**
 * <p>Description:微信用户事件管理 <／p>
 * Copyright: Copyright (c) 20017;
 * @author Dadou
 * @DateTime 2017-12-08 
 * @version 1.0
 **/
@Controller
@RequestMapping("/admin/weChatEvent")
public class WeChatEventController extends BaseController {
	// log日志
	private static Logger logger = Logger.getLogger(WeChatEventController.class);
    
    @Autowired private IWeChatEventService weChatEventService;

	/**
	 * 查看
	 * @author Dadou
	 * @DateTime 2017-12-08
	 * @version 1.0
	 * @param model
	 * @param id
	 * @return
	 */
	@GetMapping("/LookPage")
	public String LookPage(Model model, String id) {
		WeChatEvent weChatEvent = weChatEventService.selectById(id);
		model.addAttribute("weChatEvent", weChatEvent);
		return "admin/system/weChatEvent/weChatEventLook";
	}

    /**
	 * <p>Description: 列表页面</p>
	 * @author Dadou   
	 * @DateTime 2017-12-08 
	 * @version 1.0
     * @return
     */
    @GetMapping("/manager")
    public String manager() {
        return "admin/system/weChatEvent/weChatEventList";
    }

	/**
	 * <p>Description: 列表分页</p>
	 * @author Dadou   
	 * @DateTime 2017-12-08 
	 * @version 1.0
     * @return
     */
    @PostMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(WeChatEvent weChatEvent, Integer page, Integer rows, String sort, String order) {
        EntityWrapper<WeChatEvent> wrapper = new EntityWrapper<WeChatEvent>();
        wrapper.setEntity(weChatEvent);
        Page<WeChatEvent> pages = getPage(page, rows, sort, order);
        pages = weChatEventService.selectPage(pages,wrapper);
        return pageToPageInfo(pages);
    }
    
    /**
	 * <p>Description: 添加页面</p>
	 * @author Dadou   
	 * @DateTime 2017-12-08 
	 * @version 1.0
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/system/weChatEvent/weChatEventAdd";
    }
    
    /**
	 * 
	 * <p>Title: pageList</p>
	 * <p>Description: WAP调用分页列表</p>
	 * @author Dadou   
	 * @DateTime 2017-12-08 
	 * @param weChatEvent  后台
	 * @return
	 */
    @PostMapping("/pageList")
    @ResponseBody
    public Page<WeChatEvent> pageList(WeChatEvent weChatEvent,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10")  Integer rows,
			@RequestParam(defaultValue = "id") String sort,
			@RequestParam(defaultValue = "desc") String order) {
    	EntityWrapper<WeChatEvent> wrapper = new EntityWrapper<WeChatEvent>();
    	wrapper.setEntity(weChatEvent);
    	Page<WeChatEvent> pages = getPage(page, rows, sort, order);
    	pages = weChatEventService.selectPage(pages,wrapper);
    	return pages;
    }
    
    /**
	 * 
	 * <p>Title: list</p>
	 * <p>Description: 后台调用列表</p>
	 * @author Dadou   
	 * @DateTime 2017-12-08 
	 * @param weChatEvent  后台
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public Object list(WeChatEvent weChatEvent) {
		EntityWrapper<WeChatEvent> wrapper = new EntityWrapper<WeChatEvent>();
        wrapper.setEntity(weChatEvent);
		List<WeChatEvent> list = weChatEventService.selectList(wrapper);
		return list;
	}
	
	/**
	 * 
	 * <p>Title: appList</p>
	 * <p>Description: API接口调用</p>
	 * @author Dadou   
	 * @DateTime 2017-12-08 
	 * @param weChatEvent
	 * @param json  
	 * @return
	 */
	@RequestMapping("/app/list")
	@ResponseBody
	public Object appList(WeChatEvent weChatEvent, String json) {
		if (StringUtils.isNotBlank(json)) {
			try {
				json = Des3Util.decode(json);
				json=json.replaceAll("'", "\"");
				weChatEvent= JsonUtils.jsonToPojo(json, WeChatEvent.class);
				EntityWrapper<WeChatEvent> wrapper = new EntityWrapper<WeChatEvent>();
        		wrapper.setEntity(weChatEvent);
				List<WeChatEvent> list = weChatEventService.selectList(wrapper);
				return success(list.isEmpty()?null:list);
			} catch (Exception e) {
				return error(e.getMessage());
			}
		}
		return error("参数不能为空");
		
	}
	


    /**
	 * <p>Description: 添加</p>
	 * @author Dadou   
	 * @DateTime 2017-12-08 
	 * @version 1.0
     * @param 
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(WeChatEvent weChatEvent, String json) {
		if (StringUtils.isNotBlank(json)) {
			try {
				json = Des3Util.decode(json);
				json=json.replaceAll("'", "\"");
				weChatEvent=JsonUtils.jsonToPojo(json, WeChatEvent.class);
			} catch (Exception e) {
				return error(e.getMessage());
			}
		}
		weChatEvent.setCreatedTime(new Date());
		weChatEvent.setCreatedEditor(this.getShiroUser().getName());
        boolean b = weChatEventService.insert(weChatEvent);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

    /**
	 * <p>Description: 删除</p>
	 * @author Dadou   
	 * @DateTime 2017-12-08 
	 * @version 1.0
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        boolean b = weChatEventService.deleteById(Long.parseLong(id));
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }
    
    /**
	 * <p>Description: 批量删除</p>
	 * @author Dadou   
	 * @DateTime 2017-12-08 
	 * @version 1.0
	 * @param ids
	 * @return
	 */
	@PostMapping("/deleteIds")
	@ResponseBody
	public Object deleteIds(String ids) {
		if (StringUtils.isNotBlank(ids)) {
			String[] split = ids.split(",");
			int num = 0;
			for (String id : split) {
				boolean b = weChatEventService.deleteById(Long.parseLong(id));
				if (b) {
					num++;
				}
			}
			if (num > 0) {
				return renderSuccess("删除"+num+"条记录成功！");
			} else {
				return renderError("删除失败！");
			}
		}
		return renderError("参数错误");
	}

    /**
     * 编辑
     * @author Dadou   
	 * @DateTime 2017-12-08 
	 * @version 1.0
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, String id) {
        WeChatEvent weChatEvent = weChatEventService.selectById(id);
        model.addAttribute("weChatEvent", weChatEvent);
        return "admin/system/weChatEvent/weChatEventEdit";
    }

    /**
     * 编辑
     * @author Dadou   
	 * @DateTime 2017-12-08 
	 * @version 1.0
     * @param 
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(WeChatEvent weChatEvent, String json) {
		if (StringUtils.isNotBlank(json)) {
			try {
				json = Des3Util.decode(json);
				json=json.replaceAll("'", "\"");
				weChatEvent=JsonUtils.jsonToPojo(json, WeChatEvent.class);
			} catch (Exception e) {
				return error(e.getMessage());
			}
		}
		weChatEvent.setUpdatedTime(new Date());
		weChatEvent.setUpdatedEditor(this.getShiroUser().getName());
        boolean b = weChatEventService.updateById(weChatEvent);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }
    
}
